package com.example.validatestudy.demos.tread.sync;

public class SynchronizedTest02 {
    /**
     * synchronized
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("---synchronized修饰静态方法 : 两个线程先后执行-------");
        staticPrintTest();
    }

    public static void staticPrintTest(){
        try {
            Runnable r1 = ()-> staticPrint(10);
            Runnable r2 = ()-> staticPrint(10);
            Thread t1 = new Thread(r1);
            Thread t2 = new Thread(r2);
            t1.start();
            t2.start();
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void staticPrint(int total){
        while(0 < total){
            System.out.println(Thread.currentThread().getName()+ "-staticPrint():" + total--);
        }
    }

}
